package com.rc.evcharger.common.utils;

import java.util.Calendar;

/**
 * Create by Yang on 2021/11/19
 */
public class YkrDataUtil {


    /**
     * 获取当前时间的BCD码帧格式
     */
    public static String dateToCP56time2a() {

        StringBuffer stringBuffer = new StringBuffer();

        Calendar instance = Calendar.getInstance();

        //毫秒
        stringBuffer.append("00").append(DataUtil.SPLIT).append("00").append(DataUtil.SPLIT);

        //秒
        String sed = Integer.toHexString(instance.get(Calendar.SECOND));
        if (sed.length() == 1) {
            sed = "0" + sed;
        }
        stringBuffer.append(sed).append(DataUtil.SPLIT);

        //分
        String min = Integer.toHexString(instance.get(Calendar.MINUTE));
        if (min.length() == 1) {
            min = "0" + min;
        }
        stringBuffer.append(min).append(DataUtil.SPLIT);

        //时
        String hour = Integer.toHexString(instance.get(Calendar.HOUR_OF_DAY));
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        stringBuffer.append(hour).append(DataUtil.SPLIT);


        //日
        String day = Integer.toHexString(instance.get(Calendar.DAY_OF_MONTH));
        if (day.length() == 1) {
            day = "0" + day;
        }
        stringBuffer.append(day).append(DataUtil.SPLIT);

        //月
        String month = Integer.toHexString(instance.get(Calendar.MONTH) + 1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        stringBuffer.append(month).append(DataUtil.SPLIT);


        //年
        String year = instance.get(Calendar.YEAR) + "";

        String yearHex = Integer.toHexString(Integer.valueOf(year.substring(2)));
        if (yearHex.length() == 1) {
            yearHex = "0" + yearHex;
        }

        stringBuffer.append(yearHex).append(DataUtil.SPLIT);

        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        String s = dateToCP56time2a();
        System.out.println(s);
    }
}
